package com.pj.compose.ui.page.widget

import android.graphics.Bitmap
import android.view.View
import android.webkit.WebView
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.google.accompanist.web.AccompanistWebViewClient
import com.google.accompanist.web.LoadingState
import com.google.accompanist.web.WebView
import com.google.accompanist.web.rememberWebViewState
import com.pj.compose.ui.compose.AppTopBarColumn


@Composable
fun EChartPage(navController: NavController) {
    var webView by remember { mutableStateOf<WebView?>(null) }
//    val state = rememberWebViewState(url = "https://www.baidu.com")
    val state = rememberWebViewState(url = "file:///android_asset/echart/charts.html")

  /*  if (state.loadingState == LoadingState.Finished) {
        webView?.run { loadUrl("javascript:setData('$source','$dataJson','$chartTextColor')") }
    }*/

    AppTopBarColumn(modifier = Modifier, title = "EChartPage", backClick = { navController.popBackStack() }) {
        Spacer(modifier = Modifier.height(30.dp))
        val webClient = remember  {
            object : AccompanistWebViewClient() {
                override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                    super.onPageStarted(view, url, favicon)
                }

                override fun onPageFinished(view: WebView?, url: String?) {
                    super.onPageFinished(view, url)
//                    "图表网页加载完成 onPageFinished".logd()
                }
            }
        }

        WebView(
            state = state,
            onCreated = {
                webView = it
                it.settings.apply {
                    javaScriptEnabled = true
                    javaScriptCanOpenWindowsAutomatically = true
                    defaultTextEncodingName = "utf-8"
                    setSupportZoom(false)
                    domStorageEnabled = true
                    displayZoomControls = false
                }
                it.setBackgroundColor(android.graphics.Color.TRANSPARENT)
                it.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
            },
            onDispose = {
                webView?.destroy()
                webView = null
            },
            captureBackPresses = false,
            client = webClient,
            modifier = Modifier
                .fillMaxWidth()
                .height((300 - 30).dp)
        )
    }
}